package org.example.laboratorymanagement.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.laboratorymanagement.pojo.Notice;
import org.example.laboratorymanagement.pojo.PageBean;
import org.example.laboratorymanagement.pojo.Result;
import org.example.laboratorymanagement.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@Slf4j
@RequestMapping("/notices")
@RestController
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("批量删除公告ID操作，ids：{}",ids);
        noticeService.delete(ids);
        return Result.success();
    }

    @PostMapping
    public Result insert(@RequestBody Notice notice){
        log.info("新增公告：{}",notice);
        noticeService.insert(notice);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Notice notice){
        log.info("修改公告：{}",notice);
        noticeService.update(notice);
        return Result.success();
    }

    @GetMapping
    public Result pageNotices(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              String title){
        log.info("分页查询公告，参数：{},{},{}",page, pageSize, title);
        PageBean pageBean = noticeService.pageNotices(page, pageSize, title);
        return Result.success(pageBean);
    }

    @GetMapping("/selectAll")
    public Result selectAll(){
        log.info("查询公开公告：");
        List<Notice> list = noticeService.selectAll();
        return Result.success(list);
    }

}
